<?
require_once "rwtable.php";

$uri = $_SERVER['REQUEST_URI'];
$target = false;
foreach ($rewrite as $rule) {
    if (preg_match($rule['RULE'], $uri)) {
        $target = $rule;
    }
}
if (!$target) {
    header('Location:/error.php');
    die();
}

$out = preg_replace($target['RULE'], $target['COND'], $uri);
$location = $target['PATH'];
$filename = $_SERVER['DOCUMENT_ROOT'] . $location;
$data = parse_url($out);

$query = isset($data['query']) ? explode("&", $data['query']) : false;

$REQUEST = array();

if ($query) {
    foreach ($query as $string) {
        list($key, $val) = explode("=", $string);
        $REQUEST[$key] = $val;
    }
}

if (file_exists($filename)) {
    require_once $filename;
} else {
    header('Location:/error.php');
    die();
}
?>